#include <bits/stdc++.h>
using namespace std;

#define int long long
int n, m, ans;
pair<int, int> p[100001];
priority_queue<int, vector<int>, greater<int>> pq;

signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> p[i].first >> p[i].second;
    sort(p + 1, p + n + 1);
    for (int i = 1; i <= n; i++) {
        if (m < p[i].first)
            m++, ans += p[i].second, pq.push(p[i].second);
        else if (pq.top() < p[i].second)
            ans += p[i].second - pq.top(), pq.pop(), pq.push(p[i].second);
    }
    cout << ans;
    return 0;
}